package com.system.util.freemarker.template;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.component.domain.Note;
import com.component.service.NoteService;
import com.system.config.freemarker.DirectiveHandler;
import com.system.config.freemarker.FreemarkerScan;
import com.system.config.freemarker.TemplateDirective;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname TitleListTemplate
 * @Description TODO
 * @Date 2023/9/17 4:05
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@FreemarkerScan("titlelist")
public class TitleListTemplate extends TemplateDirective {

    @Autowired
    private NoteService noteService;

    @Override
    public String getName() {
        return "titlelist";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        Integer pageNum=handler.getInteger("pageNum");
        Integer pageSize=handler.getInteger("pageSize");
        String keyword=handler.getString("keyword");
        IPage<Note> page=noteService.notePageListByKeyWord(pageNum,pageSize,null,keyword,null,false);
        System.out.println(JSONObject.toJSONString(page));
        Map<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("records",JSONObject.toJSONString(page.getRecords()));
        handler.put(RESULT,map).render();
    }
}